package com.csust.automaticteach.controller;

import com.csust.automaticteach.common.BaseResponse;
import com.csust.automaticteach.common.ResultUtils;
import com.csust.automaticteach.utils.ShareEncryptUtil;
import jakarta.annotation.Resource;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassNameCommonController
 * @Description TODO
 * @Author钰玟
 * @Date2025/8/1 下午2:56
 * @Version 1.0
 **/
@RestController
@RequestMapping("/share")
public class ShareController {
    @Resource
    private ShareEncryptUtil encryptUtil;

    // 生成分享 Token 接口（供前端调用，生成分享链接）
    @PostMapping("/generateToken")
    public BaseResponse<String> generateToken(@RequestBody ShareGenerateRequest shareGenerateRequest) {
        // 参数：bizType（业务类型）、bizId（数据ID）、expireSeconds（有效期，如 86400=24小时）
        String token = encryptUtil.generateShareToken(
                shareGenerateRequest.getBizType(),
                shareGenerateRequest.getBizId(),
                shareGenerateRequest.getExpireSeconds()
        );
        return ResultUtils.success(token);
    }

    // 验证并解密 Token 接口（供分享页面调用）
    @GetMapping("/verifyToken")
    public BaseResponse<ShareEncryptUtil.ShareInfo> verifyToken(@RequestParam("token") String token) {
        ShareEncryptUtil.ShareInfo info = encryptUtil.decryptAndVerifyToken(token);
        return ResultUtils.success(info);
    }
}

    // DTO 类
    @Data
    class ShareGenerateRequest {
        private String bizType; // 如 "doc"、"order"
        private String bizId;   // 如 "123"
        private long expireSeconds = 86400; // 默认 24 小时过期
}
